package com.atguigu.gmall.mq.controller;

import com.atguigu.gmall.common.RabbitService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.mq.config.DeadLetterMqConfig;
import com.atguigu.gmall.mq.config.DelayedMqConfig;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("mq/")
public class MqController {

    @Autowired
    private RabbitService rabbitService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("sendMsg")
    public Result sendMsg(){
        //  发送消息.
        rabbitService.sendMsg("exchange.confirm","routing.confirm666","死鬼回来接客了.");
        return Result.ok();
    }

    //  设置发送延迟消息的控制器.
    @GetMapping("sendDeadLettle")
    public Result sendDeadLettle(){
        //  什么时候发送的消息.
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        rabbitService.sendMsg(DeadLetterMqConfig.exchange_dead,DeadLetterMqConfig.routing_dead_1,"dead...");
        System.out.println("发送消息时间：\t"+simpleDateFormat.format(new Date()));
        return Result.ok();
    }

    @GetMapping("sendDelay")
    public Result sendDelay(){
        //  什么时候发送的消息.
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //  rabbitService.sendMsg(DeadLetterMqConfig.exchange_dead,DeadLetterMqConfig.routing_dead_1,"dead...");
        //        this.rabbitTemplate.convertAndSend(DelayedMqConfig.exchange_delay, DelayedMqConfig.routing_delay, "atguigu", message -> {
        //            //  设置延迟时间. 10 s ;
        //            message.getMessageProperties().setDelay(10000);
        //            System.out.println("发送消息时间：\t"+simpleDateFormat.format(new Date()));
        //            return message;
        //        });
        //
        this.rabbitService.sendDelayMsg(DelayedMqConfig.exchange_delay, DelayedMqConfig.routing_delay, "atguigu",10);
        return Result.ok();
    }


}
